/*
 * Created on 2005-11-15
 *
 */
package bma.common.langutil.core;

import java.util.concurrent.TimeUnit;

/**
 * 对象工具类
 * 
 * @author 关中
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 */
public class ObjectUtil {

	public static boolean notEquals(Object o1, Object o2) {
		if (o1 == null)
			return o2 != null;
		return !o1.equals(o2);
	}

	public static boolean equals(Object o1, Object o2) {
		if (o1 == null)
			return o2 == null;
		return o1.equals(o2);
	}

	public static String getString(Object obj) {
		if (obj == null)
			return "null";
		return obj.toString();
	}

	public static String toString(Object obj) {
		if (obj == null)
			return null;
		return obj.toString();
	}

	public static int hashCode(Object... fields) {

		int result = 0;
		if (fields != null) {
			for (Object f : fields) {
				result = 29 * result + (f == null ? 0 : f.hashCode());
			}
		}

		return result;
	}

	public static void notifyWakeup(Object obj) {
		if (obj == null)
			return;
		synchronized (obj) {
			obj.notifyAll();
		}
	}

	/**
	 * 等待
	 * 
	 * @param obj
	 * @param time
	 * @return
	 */
	public static void waitFor(Object obj, long time, TimeUnit unit) {
		waitFor(obj, unit.toMillis(time));
	}

	public static void waitFor(Object obj, long time) {
		synchronized (obj) {
			try {
				obj.wait(time);
			} catch (InterruptedException e) {
				throw new ThreadInterruptedException(e);
			}
		}
	}

}
